(function(window, $){
	
	var loginClick, loginClickFun, timer = null, 
		LoginBto = "#bto-login", msgContainer = "#msgContainer", cookieUser = "cookieUser" ;
	var alertMsg = "<div id='msg' class='alert alert-block alert-{{color}}'><i class='ace-icon fa fa-{{icon}}'></i>　{{msg}}</div>" ;
	
	$(function(){
		initCookie() ;
		
		loginClick =  $(LoginBto).bind("click", loginClickFun = function() {
			loginValid() ;
		});
		
	});
	
	
	function loginValid() {
		$(msgContainer).empty() ;
		
		var username = $("input[type=text][name=login-username]").val() ;
		var password = $("input[type=password][name=login-password]").val() ;
		var Remember = $("#Remember").is(":checked") ;
		
		if(undefined == username || "" == username.trim()) {
			showMsg("warning", "warning", "用户名不用为空！")
			return ;
		}
		if(undefined == password || "" == password.trim()) {
			showMsg("warning", "warning", "密码不用为空！")
			return ;
		}
		if(/\s/.test(username) || /\s/.test(password)) {
			showMsg("warning", "warning", "用户名和密码不允许出现空格！")
			return ;
		}
		
		loginClick.unbind("click");
			
		$(msgContainer).html($(alertMsg.replace(/{{color}}/g, "info").replace(/{{icon}}/g, "refresh fa-spin").replace(/{{msg}}/g, "登录验证中，请稍等...")));
		
		$.post($.webapp.ctx+"/front/login", {"username": username, "password": password}, function(result) {
			if(result.status) {
				$(msgContainer).empty() ;
				showMsg("success", "check", result.message) ;
				
				if(Remember) {
					setCookie(cookieUser, $.base64.encode(username)+"[#]"+$.base64.encode(password));
				} else {
					removeCookie(cookieUser);
				}
				
				setTimeout(function(){		
					window.location.replace($.webapp.ctx+"/admin/index");
				},500);
			} else {
				$(msgContainer).empty() ;
				showMsg("danger", "times", (undefined != result.message?result.message:"登录异常或者该账号已被登录！")) ;
				removeCookie(cookieUser);
				loginClick.bind("click", loginClickFun) ;
			}
		});		
		
		
	} ;
	
	function showMsg(color, icon, msg) {
		var msg = $(alertMsg.replace(/{{color}}/g, color).replace(/{{icon}}/g, icon).replace(/{{msg}}/g, msg)).slideDown(function(){
			clearTimeout(timer);
			timer = setTimeout(function(){
				msg.slideUp(function(){$(msgContainer).empty() ;});
			}, 5000);
		}).appendTo(msgContainer);
	}
	
	
	function initCookie() {
		var loginUser = $.cookie(cookieUser);
		if (undefined != loginUser && "" != loginUser) {
			var splits = loginUser.split("[#]");
			$("input[type=text][name=login-username]").val($.base64.decode(splits[0]))
			$("input[type=password][name=login-password]").val($.base64.decode(splits[1]))
			$("#Remember").prop("checked", true);
		} else {
			$("input[type=text][name=login-username]").val("");
			$("input[type=password][name=login-password]").val("");
			$("#Remember").prop("checked", false);
		}
	}
	function setCookie(name, value) {
		$.cookie(name, value, {
			isStrict: true,
			expires : 30,
			path : "/"
		});
	}
	function removeCookie(name) {
		$.cookie(name, null, {
			expires : -1,
			path : "/"
		});
	}
	
	
})(window, jQuery);